﻿// DPImmOperand2.cs
// Contains a class for an immediate operand 2 for a data processing instruction

using System;


namespace armsim
{
    /// <summary>
    /// Class for an Immediate Operand for a Data Processing Instruction
    /// </summary>
    public class DPImmOperand2 : DPOperand2
    {
        /// <summary>
        /// The number for the immediate to be rotated by (x2). 4 bits
        /// </summary>
        public uint Rot { get; protected set; }

        /// <summary>
        /// The immediate value contained in 8 bits
        /// </summary>
        public uint Immediate { get; protected set; }

        /// <summary>
        /// Initializes a new DPImmOperand2 with the Rot and Immediate values set
        /// </summary>
        /// <param name="instruction">values are extracted from here</param>
        public DPImmOperand2(uint instruction) : base(instruction)
        {
            this.Rot = VirtualMemory.ExtractBits(instruction, 8, 11);
            this.Immediate = VirtualMemory.ExtractBits(instruction, 0, 7);
        }
        
        /// <summary>
        /// Computes the immediate value to be used in the instruction.
        /// </summary>
        /// <param name="cpu">the VirtualCPU to retrieve values from</param>
        /// <returns>the final immediate value</returns>
        public override uint ComputeValue(VirtualCPU cpu)
        {
            return BarrelShifter.Ror(this.Immediate, this.Rot*2);
        }

        /// <summary>
        /// Converts the Immediate Operand into assembly language
        /// </summary>
        /// <param name="cpu">the VirtualCPU to retrieve values from</param>
        /// <returns>a string with the number in assembly language style. base 10.</returns>
        public override string ToAssembly(VirtualCPU cpu)
        {
            return String.Format("#{0}", this.ComputeValue(cpu));
        }
    }
}
